<template>
  <div>
    <Nav :title="listName"></Nav>
    <div class="container">
      <div class="item_sort">
        <div
          @click="sortBy(0)"
          :class="['sale', 'co', { active: sort.by === 0 }]"
        >
          <span>销量</span>
        </div>
        <div
          @click="sortbyPrice"
          :class="['price', 'co', { active: sort.by === 1 }]"
        >
          <span>价格</span>
          <div
            :class="[
              'price_up',
              'triangel',
              { active: sort.by === 1 && sort.upDown === 0 },
            ]"
          ></div>
          <div
            :class="[
              'price_down',
              'triangel',
              { active: sort.by === 1 && sort.upDown === 1 },
            ]"
          ></div>
        </div>
        <div
          @click="sortBy(2)"
          :class="['new', 'co', { active: sort.by === 2 }]"
        >
          <span>新品</span>
        </div>
      </div>
      <!-- 商品列表数据 -->
      <div class="list_wrapper">
        <div class="list_item" v-for="item in list" :key="item.id" @click="goDetail(item.id)">
          <div class="img1">
            <img :src="item.pic" alt="" />
          </div>
          <div class="list_info">
            <p class="name">
              {{item.name}}
            </p>
            <p class="info_laber"> {{item.characteristic? item.characteristic.length < 10 ?item.characteristic.length:item.characteristic.substr(0, 10)+'...' : '暂无描述'}}</p>
            <div class="price">
              <p class="price_box">￥{{item.minPrice}}</p>
              <p class="price_produce">
                <i>领券3000-100</i>
                <i>赠品</i>
              </p>
              <p class="price_num">
                <em>800+评价</em>
                <em>好评率99%</em>
              </p>
              <p class="price_shop">
                <em>永轩电脑专卖店</em>
                <img src="../../assets/goods.png" alt="" />
                <i>进店</i>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { fetchItem } from '_api'
import Nav from '../../components/Nav.vue'
export default {
  components: { Nav },
  data () {
    return {
      listName: '列表页',
      sort: {
        by: 0, // 0 销量 1价格 2新品
        upDown: 0
      },
      list: [] // 列表数据
    }
  },
  created () {
    this.fetchItem()
  },
  methods: {
    sortBy (n) {
      this.sort.by = n
      this.fetchItem()
    },
    // 点击价格 如果不是从销量点击过来 按照升序 否则就是降序
    sortbyPrice () {
      if (this.sort.by !== 1) {
        this.sort.by = 1
        this.sort.upDown = 0
      } else {
        if (this.sort.upDown === 0) {
          this.sort.upDown = 1
        } else {
          this.sort.upDown = 0
        }
      }
      this.fetchItem()
    },
    // 请求列表数据
    fetchItem () {
      fetchItem({
        k: this.$route.query.keys, // 关键词字段
        orderBy: this.orderBy
      }).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          this.list = res.data.data.result
        }
      })
    },
    // 跳转去详情页
    goDetail (id) {
      this.$router.push(`/detail/${id}`)
    }
  },
  // 依赖数据获取不同的字段
  computed: {
    // 点击请求相应orderby
    orderBy () {
      let orderBy = 'ordersDown'
      switch (this.sort.by) {
        case 0:
          // 销量排序
          orderBy = 'ordersDown'
          break
        case 1:
          if (this.sort.upDown === 0) {
            orderBy = 'priceUp'
          } else {
            orderBy = 'priceDown'
          }
          break
        case 2:
          orderBy = 'addedDown'
          break
        default:
          break
      }
      return orderBy
    }
  }

}
</script>

<style lang="scss" scoped>
.container {
  padding-top: 1.2rem;
  .item_sort {
    display: flex;
    width: 100%;
    height: 1.173333rem;
    background-color: #fff;
    justify-content: space-around;
    align-items: center;
    .co {
      font-size: .426667rem;
      height: 1.173333rem;
      line-height: 1.173333rem;
      span {
        display: inline-block;
        height: 100%;
      }
      &.active {
        color: #ffdb47;
      }
    }
  }
}

// 三角
// .price{
//   position: relative;
//   .triangel{
//   width:0;
//   height: 0;
//   border:.266667rem solid transparent;
//   position: absolute;
//   right: -15px;
//   .price_up{
//     border-bottom-color: #333;
//     &.active{
//       border-bottom-color: #f33;
//     }
//   }
//   .price_down{
//     border-bottom-color: #333;
//   }
// }
// }
.triangel {
  width: 0;
  height: 0;
  border-left: 0.066667rem solid transparent;
  border-right: 0.066667rem solid transparent;
  position: absolute;
  right: -0.2rem;
}
.price {
  position: relative;
}
.price_up {
  border-bottom: 0.133333rem solid #cccc;
  top: 0.2rem;
  &.active {
    border-bottom-color: #ffdb47;
  }
}
.price_down {
  border-top: 0.133333rem solid #cccc;
  top: 0.373333rem;
  &.active {
    border-top-color: #ffdb47;
  }
}

.list_wrapper {
  background-color: #fff;
  padding: 0.266667rem;
  .list_item {
    display: flex;
    .img1 {
      display: inline-block;
      width: 2.666667rem;
      height: 2.666667rem;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .list_info {
      padding-bottom: .133333rem;
      border-bottom:1px solid #ccc;
      flex: 1;
      .name {
        margin: 0.026667rem 0 0.146667rem;
        overflow: hidden;
        font-size: 0.346667rem;
        height: 0.933333rem;
        line-height: 0.466667rem;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      .info_laber {
        font-size: 0.32rem;
        color: #999;
        margin: -0.098667rem 0 0.266667rem;
      }
    }
  }
}
.price {
  &_box {
    font-size: 0.453333rem;
    color: #f60;
    line-height: 0.613333rem;
  }
  &_produce {
    i {
      height: 0.32rem;
      line-height: 24px;
      margin: 0 0.053333rem 0 0;
      padding: 0 0.053333rem;
      color: #fff;
      background-color: #f60;
      font-size: 0.24rem;
      border-radius: 2px;
    }
  }
  &_num {
    margin: 0.226667rem 0 0 0;
    em {
      color: #999;
      margin: 0 0.32rem 0 0;
      font-weight: normal;

      font-size: .32rem;
    }
  }
  &_shop{
    color: #999;
    height: .373333rem;
    line-height: .373333rem;
    margin-top: .093333rem;
    font-size: .32rem;
    img{
      display: inline-block;
      height: .373333rem;
      width: .373333rem;
      margin-left: .133333rem;
      vertical-align: top;
    }
    i{
      display: inline-block;
      font-size: .293333rem;
      position: relative;
      margin-left: .133333rem;
      &:after {
        position: absolute;
        right:-.2rem;
        top: .08rem;
        content: "";
        width: .12rem;
        height: .24rem;
        display: block;
        background: url("../../assets//arrow.png") no-repeat center center;
        background-size: 100%;
      }
    }
  }
}
</style>
